CREATE OR REPLACE FUNCTION func_dropIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_audent_jt_0' AND schemaname = 'public';

      IF VCOUNT = 1 THEN
          DROP INDEX IX_AUDENT_JT_0;
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_dropIndexElement();
 DROP FUNCTION func_dropIndexElement();

CREATE OR REPLACE FUNCTION func_createIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_audent_jt_0' AND schemaname = 'public';

      IF VCOUNT = 0 THEN
          CREATE INDEX IX_AUDENT_JT_0 ON AUDIT_ENTITY (COD_ENTITY ASC, CLASS ASC);
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_createIndexElement();
 DROP FUNCTION func_createIndexElement();
 

CREATE OR REPLACE FUNCTION func_dropIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_event_jt_0' AND schemaname = 'public';

      IF VCOUNT = 1 THEN
          DROP INDEX IX_EVENT_JT_0;
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_dropIndexElement();
 DROP FUNCTION func_dropIndexElement();

CREATE OR REPLACE FUNCTION func_createIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_event_jt_0' AND schemaname = 'public';

      IF VCOUNT = 0 THEN
          CREATE INDEX IX_EVENT_JT_0 ON EVENT (TS_EVENT ASC);
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_createIndexElement();
 DROP FUNCTION func_createIndexElement();
 
 
CREATE OR REPLACE FUNCTION func_dropIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_excrec_jt_1' AND schemaname = 'public';

      IF VCOUNT = 1 THEN
          DROP INDEX IX_EXCREC_JT_1;
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_dropIndexElement();
 DROP FUNCTION func_dropIndexElement();

CREATE OR REPLACE FUNCTION func_createIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_excrec_jt_1' AND schemaname = 'public';

      IF VCOUNT = 0 THEN
          CREATE INDEX IX_EXCREC_JT_1 ON EXCEPTION_RECORD (TIMESTAMP_EXCEPTION ASC);
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_createIndexElement();
 DROP FUNCTION func_createIndexElement();
 

CREATE OR REPLACE FUNCTION func_dropIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_phinst_jt_1' AND schemaname = 'public';

      IF VCOUNT = 1 THEN
          DROP INDEX IX_PHINST_JT_1;
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_dropIndexElement();
 DROP FUNCTION func_dropIndexElement();

CREATE OR REPLACE FUNCTION func_createIndexElement() RETURNS void AS $$
DECLARE
    VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM pg_indexes WHERE indexname = 'ix_phinst_jt_1' AND schemaname = 'public';

      IF VCOUNT = 0 THEN
          CREATE INDEX IX_PHINST_JT_1 ON PHASE_INSTANCE (TS_START ASC);
      END IF;
      RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_createIndexElement();
 DROP FUNCTION func_createIndexElement();
 
 
CREATE OR REPLACE FUNCTION func_insert_or_update() RETURNS void AS $$
DECLARE
  VCOUNT  INTEGER := 0;
BEGIN
  SELECT COUNT(*) INTO VCOUNT FROM PURGE_CONFIG WHERE COD_PURGE_CONFIG = 6;
  IF VCOUNT = 1 THEN
    UPDATE PURGE_CONFIG SET COD_PURGE_CONFIG = 6, CRON_EXPRESSION = '0 4 * * *', DAYS_TO_KEEP = 30, DESCR = 'EXPURGO DE OCORRÊNCIAS DE PROCESSAMENTO', ENABLED = true, MAX_WINDOW_SIZE = 100, NAME = 'EVENT', PURGER_BEAN_NAME = 'eventPurger' WHERE COD_PURGE_CONFIG = 6;
  ELSE
    INSERT INTO PURGE_CONFIG (COD_PURGE_CONFIG, CRON_EXPRESSION, DAYS_TO_KEEP,                                     DESCR, ENABLED, MAX_WINDOW_SIZE,    NAME, PURGER_BEAN_NAME)
                               VALUES (               6,     '0 4 * * *',           30, 'EXPURGO DE OCORRÊNCIAS DE PROCESSAMENTO',    true,             100, 'EVENT',    'eventPurger');
  END IF;
  RETURN;
END;

$$ LANGUAGE plpgsql;
SELECT func_insert_or_update();
 DROP FUNCTION func_insert_or_update();